<!-- jQuery 2.0.2 -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<!-- Bootstrap -->
<script src="/javascripts/js/bootstrap.min.js" type="text/javascript"></script>
<!-- Jquery -->
<!--  <script src="/javascripts/jquery.js" type="text/javascript"></script> -->
<!-- InputMask -->
<script src="/javascripts/js/plugins/input-mask/jquery.inputmask.js"
	type="text/javascript"></script>
<script
	src="/javascripts/js/plugins/input-mask/jquery.inputmask.date.extensions.js"
	type="text/javascript"></script>
<script
	src="/javascripts/js/plugins/input-mask/jquery.inputmask.extensions.js"
	type="text/javascript"></script>
<!-- date-range-picker -->
<script src="/javascripts/js/plugins/daterangepicker/daterangepicker.js"
	type="text/javascript"></script>
<!-- bootstrap color picker -->
<script
	src="/javascripts/js/plugins/colorpicker/bootstrap-colorpicker.min.js"
	type="text/javascript"></script>
<!-- bootstrap time picker -->
<script
	src="/javascripts/js/plugins/timepicker/bootstrap-timepicker.min.js"
	type="text/javascript"></script>
<!-- AdminLTE App -->
<script src="/javascripts/js/AdminLTE/app.js" type="text/javascript"></script>
<!-- Page script -->
<script type="text/javascript">

	$(function() {

		//Datemask dd/mm/yyyy
		$("#datemask").inputmask("dd/mm/yyyy", {
			"placeholder" : "dd/mm/yyyy"
		});
		//Datemask2 mm/dd/yyyy
		$("#datemask2").inputmask("mm/dd/yyyy", {
			"placeholder" : "mm/dd/yyyy"
		});
		//Money Euro
		$("[data-mask]").inputmask();

		//Date range picker
		$('#reservation').daterangepicker();
		//Date range picker with time picker
		$('#reservationtime').daterangepicker({
			timePicker : true,
			timePickerIncrement : 30,
			format : 'MM/DD/YYYY h:mm A'
		});
		//Date range as a button
		$('#daterange-btn').daterangepicker(
				{
					ranges : {
						'Today' : [ moment(), moment() ],
						'Yesterday' : [ moment().subtract('days', 1),
								moment().subtract('days', 1) ],
						'Last 7 Days' : [ moment().subtract('days', 6),
								moment() ],
						'Last 30 Days' : [ moment().subtract('days', 29),
								moment() ],
						'This Month' : [ moment().startOf('month'),
								moment().endOf('month') ],
						'Last Month' : [
								moment().subtract('month', 1).startOf('month'),
								moment().subtract('month', 1).endOf('month') ]
					},
					startDate : moment().subtract('days', 29),
					endDate : moment()
				},
				function(start, end) {
					$('#reportrange span').html(
							start.format('MMMM D, YYYY') + ' - '
									+ end.format('MMMM D, YYYY'));
				});

		//iCheck for checkbox and radio inputs
		$('input[type="checkbox"].minimal, input[type="radio"].minimal')
				.iCheck({
					checkboxClass : 'icheckbox_minimal',
					radioClass : 'iradio_minimal'
				});
		//Red color scheme for iCheck
		$('input[type="checkbox"].minimal-red, input[type="radio"].minimal-red')
				.iCheck({
					checkboxClass : 'icheckbox_minimal-red',
					radioClass : 'iradio_minimal-red'
				});
		//Flat red color scheme for iCheck
		$('input[type="checkbox"].flat-red, input[type="radio"].flat-red')
				.iCheck({
					checkboxClass : 'icheckbox_flat-red',
					radioClass : 'iradio_flat-red'
				});

		//Colorpicker
		$(".my-colorpicker1").colorpicker();
		//color picker with addon
		$(".my-colorpicker2").colorpicker();

		//Timepicker
		$(".timepicker").timepicker({
			showInputs : false
		});
	});
</script>


<script type="text/javascript">

	//Show Filter Data Form
	$(document).ready(function() {
		$(".searchLink").click(function() {
			$(".searchField").slideToggle("slow");
	
		});
	
		//hide add button onload page
		$("#dashboard").click();
	
		//hide add button when dashboard has been clicked
		$("#dashboard").click(function() {
			$(".add_listBTN").hide();
		});
	
	});
	
	
	$(document).on("submit", ".form-submission,.form-submission-search",
		function(e) {
			e.preventDefault();

			console.log("FORM SUBMITTED");
			var obj = $(this);
			handlePageLoading(obj);

		});
	
	$(document).on("click", ".load-form,.load-list,.save-form", function(e) {
		e.preventDefault();
		//console.log("101");
		var obj = $(this);
		handlePageLoading(obj);
	});

	function handlePageLoading(obj) {

		var type = "/frontend/";
		var action = "";
		var code_id = "";
		var loadTo = "page-main-content";//default
		var pageToLoad = obj.attr("title") + "/";
		var pageNo = "";
		var isFORM = false;
		var isFormSubmission = false;
		var isFormSubmissionSearch = false;
		var isDELETE = false;

		if (obj.hasClass("backend")) {
			type = "/backend/";
		}

		if (obj.hasClass("add")) {
			action = "add/";
			isFORM = true;
		} else if (obj.hasClass("edit")) {
			action = "edit/";
			isFORM = true;
		} else if (obj.hasClass("delete")) {
			if (!confirm("Are You sure that you want to delete this record?")) {
				return;
			}
			isDELETE = true;
			action = "delete/";
		}

		if (obj.hasClass("form-submission")) {
			isFormSubmission = true;
		}

		if (obj.hasClass("form-submission-search")) {
			isFormSubmissionSearch = true;
		}

		console.log(11111111111);

		var classes = obj.attr('class').split(/\s+/);
		$.each(classes, function(index, _class) {
			if (_class.indexOf("id_") == 0) {
				var classIdContainer = _class; //id_1
				code_id = classIdContainer.replace("id_", "");
				//return false;
			}
			if (_class.indexOf("load_to_") == 0) {
				var classIdContainer = _class;
				loadTo = classIdContainer.replace("load_to_", "");
			}
			if (_class.indexOf("page_no_") == 0) {
				var classIdContainer = _class;
				pageNo = classIdContainer.replace("page_no_", "");
			}

		});

		var URL = type + pageToLoad + action + code_id;

		var URL = "/<%-currentCompanyKey%>" + type + pageToLoad + action
				+ code_id;

		console.log("FULL URL : " + URL);
		console.log("LOAD TO " + loadTo);
		console.log("IS FORM SUBMISSION " + isFormSubmission);
		console.log("IS FORM SUBMISSION " + isFormSubmissionSearch);

		if (pageNo != "") {
			URL += "?pageNo=" + pageNo;
		}

		if(!isFormSubmissionSearch){
		var urlParams = $("#list_param");
			if (typeof urlParams !== "undefined"
					&& typeof urlParams.val() !== "undefined" && urlParams.val() !== "" && isFormSubmissionSearch) {
				URL += urlParams.val();
				isFormSubmissionSearch = true;
			}
		}

		if (isFormSubmissionSearch) {
			console.log("FORM SUBMISSION");
			$
					.ajax({
						url : URL,
						data : $(obj).serialize(),//#form
						type : 'POST',
						//dataType : 'json',
						success : function(data) {
							
							console
									.log("***************************************************************");
							//console.log(data);
							$("#"+ loadTo).html(data);
							//user/add
							$('.error_msg').html("");
							
							
							console.log(data);
							console.log("***********************************");
							console.log(data["record"]);

							$(".searchField").show();
	
						}
					});
		}

		else if (isFormSubmission) {
			console.log("FORM SUBMISSION");
			$
					.ajax({
						url : URL,
						data : $(obj).serialize(),//#form
						type : 'POST',
						dataType : 'json',
						success : function(data) {
							$(".error_msg").html("");
							$(".error_msg_field").hide();
							if (typeof data["record"] !== "undefined"
									&& typeof data["record"]["has_error"] !== "undefined") {
								var errorMessage = "";
								
								$(".error_msg_field").removeClass("show_errors");
								
								for (fieldWithError in data["record"]["errors"]) {
									$(".error_in_" + fieldWithError)
											.html(
													data["record"]["errors"][fieldWithError]);
									errorMessage += data["record"]["errors"][fieldWithError]
											+ "\n";
									
									$(".err_" + fieldWithError).css("display","");
									$(".err_" + fieldWithError).addClass("show_errors");

								}

								
							} else {
								//http://stardibs.com/checkorder.php?ref=referenceNumber

								$(".error_msg_field").removeClass("show_errors");
										
								var successMessageContainer = $(".form-success-message");

								successMessageContainer.fadeIn("slow");

								// specialAction(obj.attr("title"));

							}
						}
					});
		} else {
			var loadToThis = $("#" + loadTo);
			var contentSection = $("#content-section");

			loadToThis.hide();
			loadToThis.load(URL, function() {
				contentSection.removeClass();

				if (isFORM && obj.attr("title") != "user-permission") {
					contentSection
							.addClass("col-lg-10 col-lg-offset-1 text-center");

				} else {

				}
				loadToThis.fadeIn("slow");
				if (isDELETE) {
					alert("RECORD HAS BEEN SUCCESSFULLY DELETED");
				}
			});

		}

	}

	function specialAction(anything) {
		//custom implemetation
		if (anything == "user-permission") {
			//window.location = "/backend";
		}
	}
	
	
	
</script>